﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hyt.Core;
using Hyt.Core.Security;
using Hyt.Passport.Models;
using Hyt.Passport.Repositories;

namespace Hyt.Passport.Persist
{
    public class AdministratorRepository : PassportRepository<Administrator>, IAdministratorRepository
    {
        public IQueryable<Administrator> GetAdministrators(QueryOption<Administrator> option = null)
        {
            return this.PersistService.GetQuery(option);
        }

        public Administrator AddAdministrator(Administrator model)
        {
            return this.PersistService.Add(model);
        }

        public void RemoveAdministrator(Administrator model)
        {
            this.PersistService.Remove(model);
        }

        public void UpdateAdministrator(Administrator model)
        {
            this.PersistService.Update(model);
        }

        public void AddAuthorization(Administrator principal, Operation operation)
        {
            var authorization = new Authorization {Operation = operation, Principal = principal};
            this.PersistService.Add(authorization);
        }

        public void CancelAuthorization(Administrator principal, Operation operation)
        {
            var authorization =
                this.PersistService.GetQuery<Authorization>()
                    .FirstOrDefault(t => t.OperationId == operation.Id && t.PrincipalId == principal.Id);
            if (authorization != null)
            {
                this.PersistService.Remove(authorization);
            }
        }

        public IQueryable<Authorization> GetAuthorizations(QueryOption<Authorization> option = null)
        {
            return this.PersistService.GetQuery(option);
        }


    }
}
